package calorie.com.calorie.contact;

import android.content.Context;


import com.lxh.baselibray.mvp.BasePresent;
import com.lxh.baselibray.mvp.IBaseView;
import com.lxh.baselibray.net.NetCallBack;
import com.lxh.baselibray.net.ServiceGenerator;
import com.lxh.baselibray.util.SPUtils;

import calorie.com.calorie.api.ApiService;
import calorie.com.calorie.constant.Constant;
import calorie.com.calorie.request.BaseRequest;
import calorie.com.calorie.response.AssertInfomationResponce;
import calorie.com.calorie.response.ResultDTO;
import calorie.com.calorie.response.UserSportResponse;
import retrofit2.Call;
import retrofit2.Response;


public class SaveSportDataContact {




    public static class SaveSportDataPresent extends BasePresent<ISaveSportDataView> {
        private  GetAssetsContact.IgetAssets igetAssets;
        public SaveSportDataPresent(GetAssetsContact.IgetAssets igetAssets) {
            this.igetAssets = igetAssets;
        }

        public void saveSportData(double calorie, double heartRate, double stepNumber, double mileage,double time, int userId) {
            BaseRequest request = new BaseRequest();
            request.put("calorie", calorie);
            request.put("heartRate", heartRate);
            request.put("stepNumber", stepNumber);
            request.put("mileage", mileage);
            request.put("userId", userId);
            request.put("activityTime",time);
            ApiService service= ServiceGenerator.createService(ApiService.class);
            service.saveSportData(request).enqueue(new NetCallBack<UserSportResponse>() {
                @Override
                public void onSuccess(Call<UserSportResponse> call, Response<UserSportResponse> response) {
                    if(getView()!=null){
                        getView().getSaveSportDataResult(response);
                    }
                }

                @Override
                public void onFailed() {

                }
            });
        }

        public void getAssets(Context context) {
            try {
                ApiService service = ServiceGenerator.createService(ApiService.class);

                int userId = SPUtils.getInt(Constant.USER_ID, -1, context);
                service.getUserAssets(userId).enqueue(new NetCallBack<AssertInfomationResponce>() {
                    @Override
                    public void onSuccess(Call<AssertInfomationResponce> call, Response<AssertInfomationResponce> response) {
                        if (getView() != null) {
                            igetAssets.getAssetsResult(response);
                        }
                    }

                    @Override
                    public void onFailed() {
                        System.out.println("");
                    }
                });
            }catch (Exception e){
                e.printStackTrace();
            }
        }




        public void getTodaySportData(Context context){
            ApiService service = ServiceGenerator.createService(ApiService.class);
            int userId= SPUtils.getInt(Constant.USER_ID,-1,context);
            service.findUserTodaySportData(userId).enqueue(new NetCallBack<ResultDTO>() {
                @Override
                public void onSuccess(Call<ResultDTO> call, Response<ResultDTO> response) {
                    if (getView()!=null){
                        getView().getUserTodaySportData(response);
                    }
                }

                @Override
                public void onFailed() {

                }
            });

        }


    }

    public interface ISaveSportDataView extends IBaseView {
        void getSaveSportDataResult(Response<UserSportResponse> response);
        void getUserTodaySportData(Response<ResultDTO> response);

    }
}
